package com.soo.hoo.admin.api;

import cn.hutool.core.collection.ListUtil;
import com.soo.hoo.admin.model.dto.FileDTO;
import com.soo.hoo.admin.service.FileService;
import com.soo.hoo.core.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>文件上传控制器</p>
 *
 * @author 杨燚
 * @since 2023/6/18
 */
@Tag(name = "文件管理")
@RequestMapping("/file/")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FileController {

    private final FileService fileService;

    @PostMapping("upload")
    @Operation(summary = "上传文件")
    public Result<FileDTO> upload(@RequestParam MultipartFile file, @RequestParam(required = false) String module) {
        FileDTO fileInfo = fileService.upload(file, module);
        return Result.success("上传成功", fileInfo);
    }

    @PostMapping("batchUpload")
    @Operation(summary = "上传文件")
    public Result<List<FileDTO>> upload(@RequestParam MultipartFile[] files, @RequestParam(required = false) String module) {
        List<FileDTO> fileInfoList = fileService.upload(ListUtil.list(false, files), module);
        return Result.success("上传成功", fileInfoList);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除文件")
    public Result<Void> delete(@RequestParam Long id) {
        fileService.removeFile(id);
        return Result.success("删除成功");
    }

    @DeleteMapping("batchDelete")
    @Operation(summary = "批量删除")
    public Result<Void> delete(@RequestParam List<Long> ids) {
        fileService.removeFile(ids);
        return Result.success("删除成功");
    }

    @GetMapping("fileInfo")
    @Operation(summary = "获取文件信息")
    public Result<FileDTO> getFileInfo(@RequestParam Long id) {
        FileDTO fileInfo = fileService.getFileInfo(id);
        return Result.success(fileInfo);
    }

    @GetMapping("filesInfo")
    @Operation(summary = "获取文件信息")
    public Result<List<FileDTO>> getFileInfo(@RequestParam List<Long> ids) {
        List<FileDTO> filesInfo = fileService.getFileInfo(ids);
        return Result.success(filesInfo);
    }
}
